package com.woniuxy.woniuhis.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.SurgeryRole;
import com.woniuxy.woniuhis.exception.SurgeryRoleServiceException;
import com.woniuxy.woniuhis.mapper.SurgeryRoleMapper;
import com.woniuxy.woniuhis.service.SurgeryRoleService;

@Service
public class SurgeryRoleServiceImpl implements SurgeryRoleService{
	
	@Autowired
	SurgeryRoleMapper surgeryRoleMapper;
	
	/**
	 * 	添加手术中角色（主刀医生，第一助手医生，第二助手医生，责任护士，辅助护士......）
	 */
	@Override
	public void addSurgeryRole(SurgeryRole surgeryRole) {
		try {
			SurgeryRole sr = surgeryRoleMapper.selectSurgeryRoleByName(surgeryRole.getSurgeryRoleName());
			if(sr!= null) {
				throw new SurgeryRoleServiceException("该角色名已经存在");
			}
			surgeryRoleMapper.insertSurgeryRole(surgeryRole);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryRoleServiceException(e.getMessage());
		}
	}
	
	/**
	 * 	更新手术中角色
	 */
	@Override
	public void modifySurgeryRole(SurgeryRole surgeryRole) {
		try {
			SurgeryRole sr = surgeryRoleMapper.selectSurgeryRoleByName(surgeryRole.getSurgeryRoleName());
			if(sr!= null) {
				throw new SurgeryRoleServiceException("该角色名已经存在");
			}
			surgeryRoleMapper.updateSurgeryRole(surgeryRole);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryRoleServiceException(e.getMessage());
		}
	}
	
	/**
	 * 	查询指定手术中角色
	 */
	@Override
	public SurgeryRole findSurgeryRoleById(String id) {
		try {
			return surgeryRoleMapper.selectSurgeryRoleById(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryRoleServiceException("系统维护中");
		}
	}
	
	/**
	 * 	查询所有的手术角色
	 */
	@Override
	public PageInfo<SurgeryRole> findSurgeryRoles(SurgeryRole surgeryRole, int pageIndex, int pageSize) {
		try {
			PageHelper.startPage(pageIndex, pageSize);
			List<SurgeryRole> roles = surgeryRoleMapper.selectSurgeryRoles(surgeryRole);
			PageInfo<SurgeryRole> pageInfo = new PageInfo<SurgeryRole>(roles);
			return pageInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryRoleServiceException("系统维护中");
		}
		
	}
	/**
	 * 	查询符合条件的手术角色
	 */
	@Override
	@Cacheable("findSurgeryRoles")
	public List<SurgeryRole> findSurgeryRoles(SurgeryRole surgeryRole) {
		try {
			List<SurgeryRole> roles = surgeryRoleMapper.selectSurgeryRoles(surgeryRole);
			return roles;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryRoleServiceException("系统维护中");
		}
		
	}
	/**
	 * 	删除指定角色
	 */
	@Override
	public void delSurgeryRoleById(String id) {
		try {
			surgeryRoleMapper.deleteSurgeryRoleById(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryRoleServiceException("系统维护中");
		}
		
	}

}
